﻿using SqlSugar;

namespace Luka;

/// <summary>
/// SqlSugar扩展
/// </summary>
[NotScanned]
public static class SqlSugarExtensions
{
    /// <summary>
    /// 无锁
    /// </summary>
    /// <returns></returns>
    public static ISugarQueryable<T> NoLock<T>(this ISugarQueryable<T> sugarQueryable) where T : class
    {
        return sugarQueryable.With(SqlWith.NoLock);
    }

    /// <summary>
    /// SqlSugar 分页扩展
    /// </summary>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">行数</param>
    /// <returns></returns>
    public static PagedOutput<T> ToPagedList<T>(this ISugarQueryable<T> sugarQueryable, int pageIndex, int pageSize) where T : class
    {
        int totalNumber = 0;
        int totalPage = 0;
        var data = sugarQueryable
            .ToPageList(pageIndex, pageSize, ref totalNumber, ref totalPage);
        return new PagedOutput<T> { TotalNumber = totalNumber, TotalPage = totalPage, Rows = data };
    }

    /// <summary>
    ///  SqlSugar 分页扩展
    /// </summary>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">行数</param>
    /// <returns></returns>
    public static async Task<PagedOutput<T>> ToPagedListAsync<T>(this ISugarQueryable<T> sugarQueryable, int pageIndex, int pageSize) where T : class
    {
        RefAsync<int> totalNumber = 0;
        RefAsync<int> totalPage = 0;
        var data = await sugarQueryable
            .ToPageListAsync(pageIndex, pageSize, totalNumber, totalPage);
        return new PagedOutput<T> { TotalNumber = totalNumber, TotalPage = totalPage, Rows = data };
    }
}